package com.twetec.cms.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.twetec.cms.common.constant.ConstantsEnum;
import com.twetec.cms.model.po.system.PermissionPO;
import com.twetec.cms.model.vo.system.PermissionVO;
import com.twetec.cms.service.system.PermissionService;
import com.zgh.rbac.module.Menu;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.twetec.cms.dao.mapper.system.PermissionMapper;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @author zhxl
 * @since 2020-10-12
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, PermissionPO> implements PermissionService {

    @Override
    public List<PermissionVO> getPermissionListByUserId(Long userId) {
        List<PermissionPO> permissionPOList = baseMapper.getPermissionListByUserId(userId);
        // 返回数据
        List<PermissionVO> permissionVOList = new ArrayList<>();

        if (null != permissionPOList && !permissionPOList.isEmpty()) {
            // 循环转换成VO
            for (PermissionPO permissionPO : permissionPOList) {
                PermissionVO permissionVO = new PermissionVO();
                BeanUtils.copyProperties(permissionPO, permissionVO);

                permissionVOList.add(permissionVO);
            }
        }
        return permissionVOList;
    }

    @Override
    public boolean batchUpdatePid() {
        return baseMapper.batchUpdatePid();
    }

    @Override
    public boolean createAndUpdate(List<PermissionVO> permissionVOList) {
        // 1.判断数据是否合法
        if(null == permissionVOList || permissionVOList.isEmpty()){
            return false;
        }

        List<PermissionPO> permissionPOS = new ArrayList<>();

        // 2.对象转换
        for(PermissionVO permissionVO : permissionVOList){
            PermissionPO permissionPO = new PermissionPO();
            BeanUtils.copyProperties(permissionVO, permissionPO);
            permissionPOS.add(permissionPO);
        }
        // 4.根据工会会员id查出权限对象
        List<PermissionPO> permissionPOList = this.list();

        if(permissionPOList.isEmpty() || null == permissionPOList){
            return this.saveBatch(permissionPOS);
        }else{
            List<PermissionPO> addPermissionPOList = new ArrayList();
            List<PermissionPO> updatePermissionPOList = new ArrayList<>();
            for (PermissionPO permissionPO : permissionPOS) {
                boolean isExist = false;
                for (PermissionPO po : permissionPOList) {
                    if (permissionPO.getUnionPermissionId().equals(po.getUnionPermissionId())) {
                        isExist = true;
                        updatePermissionPOList.add(permissionPO);
                        break;
                    }
                }
                if (!isExist) {
                    addPermissionPOList.add(permissionPO);
                }
            }

            if(null != addPermissionPOList || !addPermissionPOList.isEmpty()){
                boolean createResult = this.saveBatch(addPermissionPOList);
            }
            boolean updateResult = this.updateBatchById(updatePermissionPOList);
        }
        return true;
    }

    @Override
    public List<PermissionVO> getPermissionList(List<Long> unionPermissionIds) {
        QueryWrapper<PermissionPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("UNION_PERMISSION_ID", unionPermissionIds);
        List<PermissionPO> permissionPOList = this.list(queryWrapper);

        if(null == permissionPOList || permissionPOList.isEmpty()){
            return null;
        }
        List<PermissionVO> permissionVOList = new ArrayList<>();
        for (PermissionPO permissionPO : permissionPOList){
            PermissionVO permissionVO = new PermissionVO();
            BeanUtils.copyProperties(permissionPO, permissionVO);
            permissionVOList.add(permissionVO);
        }
        return permissionVOList;
    }
}
